@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>供应商退货单编辑</title>
    <link href="~/libs/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
</head>
<body>
<div class="layui-container" style="padding: 30px 0;">
    <form class="layui-form" lay-filter="data-form">
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label">退货单编号</label>
            <div class="layui-input-block">
                <input type="text" name="returnCode" required lay-verify="required" placeholder="请输入退货单编号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">退货单名称</label>
            <div class="layui-input-block">
                <input type="text" name="returnName" required lay-verify="required" placeholder="请输入退货单名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">采购订单编号</label>
            <div class="layui-input-block">
                <input type="text" name="procurementCode" placeholder="请输入采购订单编号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">供应商</label>
            <div class="layui-input-inline" style="width: calc(70% - 45px); min-width: 180px;">
                <input type="text" name="vendor" id="vendorInput" placeholder="请输入供应商" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline" style="width: 40px; padding-left: 5px;">
                <button type="button" class="layui-btn layui-btn-primary" id="selectVendorBtn" style="padding:0 10px;height:38px;">
                    <i class="layui-icon layui-icon-search"></i>
                </button>
            </div>
        </div>
        <div id="vendorSelectLayer" style="display:none;padding:10px;">
            <table id="vendorTable" lay-filter="vendorTable"></table>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">批次号</label>
            <div class="layui-input-block">
                <input type="text" name="lotNumber" placeholder="请输入批次号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">退货日期</label>
            <div class="layui-input-block">
                <input type="text" name="returnDate" id="returnDate" placeholder="请选择退货日期" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">单据状态</label>
            <div class="layui-input-block">
                <select name="documentStatus" lay-verify="required">
                    <option value="0">草稿</option>
                    <option value="1">已确定</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="form-submit">立即提交</button>
                <button type="button" class="layui-btn layui-btn-primary" id="btn-cancel">取消</button>
            </div>
        </div>
    </form>
</div>
<script src="~/libs/scripts/layui-v2.5.6/layui/layui.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
layui.use(['form', 'layer', 'laydate', 'table'], function () {
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var $ = layui.jquery;
    var table = layui.table;
    laydate.render({
        elem: '#returnDate'
    });
    // 供应商选择按钮事件
    $(document).on('click', '#selectVendorBtn', function(){
        var vendorLayerIndex = layer.open({
            type: 1,
            title: '选择供应商',
            area: ['60vw','70vh'],
            shadeClose: true,
            content: $('#vendorSelectLayer'),
            success: function(){
                table.render({
                    elem: '#vendorTable',
                    url: '/ProductionSystem/SupplierModel/GetSupplierList',
                    cols: [[
                        {field:'supplierCode', title:'供应商编码', width:120},
                        {field:'supplierName', title:'供应商名称', minWidth:180},
                        {field:'supplierGrade', title:'等级', width:100},
                        {field:'supplierPhone', title:'电话', width:120}
                    ]],
                    page: true,
                    height: 'full-120'
                });
                table.on('row(vendorTable)', function(obj){
                    $('#vendorInput').val(obj.data.supplierName);
                    layer.close(vendorLayerIndex);
                });
            },
            end: function(){
                $('#vendorSelectLayer').hide();
            }
        });
    });
    // 取消按钮
    $('#btn-cancel').on('click', function(){
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    });
    // 获取url参数
    function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split('&');
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split('=');
            if (pair[0] == variable) { return pair[1]; }
        }
        return null;
    }
    // 如果有id参数，加载数据
    var id = getQueryVariable('id');
    if(id) {
        $.get('/SupplierReturns/GetSupplierReturns', { id: id }, function(res){
            if(res) {
                form.val('data-form', res);
                if(res.returnDate) $('#returnDate').val(layui.util.toDateString(res.returnDate, 'yyyy-MM-dd'));
            }
        });
    } else {
        // 新增时自动生成退货单编号
        var randomCode = 'RT' + Date.now() + Math.floor(Math.random()*10000).toString().padStart(4, '0');
        form.val('data-form', { returnCode: randomCode });
    }
    // 提交表单
    form.on('submit(form-submit)', function (data) {
        var formData = data.field;
        var isUpdate = formData.id && formData.id != '0';
        var url = isUpdate ? '/SupplierReturns/UpdateSupplierReturns' : '/SupplierReturns/AddSupplierReturns';
        var method = isUpdate ? 'PUT' : 'POST';
        formData.id = parseInt(formData.id) || 0;
        formData.documentStatus = parseInt(formData.documentStatus);
        $.ajax({
            url: url,
            type: method,
            contentType: 'application/json',
            data: JSON.stringify(formData),
            success: function (res) {
                var index = parent.layer.getFrameIndex(window.name);
                parent.layui.table.reload('supplier-returns-table');
                parent.layer.msg('保存成功', {icon: 1, time: 1000}, function(){
                    parent.layer.close(index);
                });
            },
            error: function () {
                layer.msg('保存失败');
            }
        });
        return false;
    });
});
</script>
</body>
</html> 